﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLNS_v1.Bus
{
    public class ChamCongNews : ChamCong
    {
        public string HoTen { get; set; }
    }
   public class ChamCong_Bus
    {
        NhanVienContextDataContext nhanvien = new NhanVienContextDataContext();
       /// <summary>
       /// danh sach cham cong
       /// </summary>
       /// <returns></returns>
       /// 
        public DataTable ListChamCong()
        {
            ChamCong cc;
            var chamcong = from pc in nhanvien.ChamCongs
                           join nv in nhanvien.NhanViens on pc.NhanVienID equals nv.NhanVienID
                           orderby pc.Nam, pc.Thang descending
                           select new ChamCongNews{ 
                              NhanVienID=pc.NhanVienID,
                              Thang=pc.Thang,
                              Nam=pc.Nam,
                              ChamCongID=pc.ChamCongID,
                              N1=pc.N1,
                              N2 = pc.N2,
                              N3 = pc.N3,
                              N4 = pc.N4,
                              N5 = pc.N5,
                              N6 = pc.N6,
                              N7 = pc.N7,
                              N8 = pc.N8,
                              N9 = pc.N9,
                              N10 = pc.N10,
                              N11 = pc.N11,
                              N12 = pc.N12,
                              N13 = pc.N13,
                              N14 = pc.N14,
                              N15 = pc.N15,
                              N16 = pc.N16,
                              N17 = pc.N17,
                              N18 = pc.N18,
                              N19 = pc.N19,
                              N20 = pc.N20,
                              N21 = pc.N21,
                              N22 = pc.N22,
                              N23 = pc.N23,
                              N24 = pc.N24,
                              N25 = pc.N25,
                              N26 = pc.N26,
                              N27 = pc.N27,
                              N28 = pc.N28,
                              N29 = pc.N29,
                              N30 = pc.N30,
                              N31 = pc.N31,
                              HoTen=nv.HoTen,
                              SoNgayDiLam=pc.SoNgayDiLam,

                           };
            return List_to_Table.ToDataTable(chamcong.ToList<ChamCongNews>());

        }
        public DataTable GetChamCong(int thang, int nam)
        {
            ChamCong cc;
            var chamcong = from pc in nhanvien.ChamCongs
                           join nv in nhanvien.NhanViens on pc.NhanVienID equals nv.NhanVienID
                           where pc.Thang==thang && pc.Nam==nam
                           orderby pc.Nam, pc.Thang descending
                           
                           select new ChamCongNews
                           {
                               NhanVienID = pc.NhanVienID,
                               Thang = pc.Thang,
                               Nam = pc.Nam,
                               ChamCongID = pc.ChamCongID,
                               N1 = pc.N1,
                               N2 = pc.N2,
                               N3 = pc.N3,
                               N4 = pc.N4,
                               N5 = pc.N5,
                               N6 = pc.N6,
                               N7 = pc.N7,
                               N8 = pc.N8,
                               N9 = pc.N9,
                               N10 = pc.N10,
                               N11 = pc.N11,
                               N12 = pc.N12,
                               N13 = pc.N13,
                               N14 = pc.N14,
                               N15 = pc.N15,
                               N16 = pc.N16,
                               N17 = pc.N17,
                               N18 = pc.N18,
                               N19 = pc.N19,
                               N20 = pc.N20,
                               N21 = pc.N21,
                               N22 = pc.N22,
                               N23 = pc.N23,
                               N24 = pc.N24,
                               N25 = pc.N25,
                               N26 = pc.N26,
                               N27 = pc.N27,
                               N28 = pc.N28,
                               N29 = pc.N29,
                               N30 = pc.N30,
                               N31 = pc.N31,
                               HoTen = nv.HoTen,
                               SoNgayDiLam = pc.SoNgayDiLam,

                           };
            return List_to_Table.ToDataTable(chamcong.ToList<ChamCongNews>());
        }
       // danh sach cham cong theo ma nhan vien
        public DataTable ListChamCong_TheoNhanVien(int manv)
        {
            var chamcong = from pc in nhanvien.ChamCongs
                           join nv in nhanvien.NhanViens on pc.NhanVienID equals nv.NhanVienID
                           where pc.NhanVienID==manv
                           orderby pc.Nam,pc.Thang descending
                           select new ChamCongNews 

                           {
                               NhanVienID = pc.NhanVienID,
                               Thang = pc.Thang,
                               Nam = pc.Nam,
                               ChamCongID = pc.ChamCongID,
                               N1 = pc.N1,
                               N2 = pc.N2,
                               N3 = pc.N3,
                               N4 = pc.N4,
                               N5 = pc.N5,
                               N6 = pc.N6,
                               N7 = pc.N7,
                               N8 = pc.N8,
                               N9 = pc.N9,
                               N10 = pc.N10,
                               N11 = pc.N11,
                               N12 = pc.N12,
                               N13 = pc.N13,
                               N14 = pc.N14,
                               N15 = pc.N15,
                               N16 = pc.N16,
                               N17 = pc.N17,
                               N18 = pc.N18,
                               N19 = pc.N19,
                               N20 = pc.N20,
                               N21 = pc.N21,
                               N22 = pc.N22,
                               N23 = pc.N23,
                               N24 = pc.N24,
                               N25 = pc.N25,
                               N26 = pc.N26,
                               N27 = pc.N27,
                               N28 = pc.N28,
                               N29 = pc.N29,
                               N30 = pc.N30,
                               N31 = pc.N31,
                               HoTen = nv.HoTen
                               ,SoNgayDiLam=pc.SoNgayDiLam,

                           };
            return List_to_Table.ToDataTable(chamcong.ToList<ChamCongNews>());
        }
        public bool UpdateChamCong(DataRow row,bool check )
        {
           var chamcong=nhanvien.ChamCongs.Single(p => p.ChamCongID == long.Parse(row["ChamCongID"].ToString()));
           
           try {
               if (check == true)
                   chamcong.SoNgayDiLam = ++chamcong.SoNgayDiLam;
               else
                   chamcong.SoNgayDiLam = --chamcong.SoNgayDiLam;
               chamcong.N1 = bool.Parse(row["N1"].ToString());
               chamcong.N2 = bool.Parse(row["N2"].ToString());
               chamcong.N3 = bool.Parse(row["N3"].ToString());
               chamcong.N4 = bool.Parse(row["N4"].ToString());
               chamcong.N5 = bool.Parse(row["N5"].ToString());
               chamcong.N6 = bool.Parse(row["N6"].ToString());
               chamcong.N7 = bool.Parse(row["N7"].ToString());
               chamcong.N8 = bool.Parse(row["N8"].ToString());
               chamcong.N9 = bool.Parse(row["N9"].ToString());
               chamcong.N10 = bool.Parse(row["N10"].ToString());
               chamcong.N11 = bool.Parse(row["N11"].ToString());
               chamcong.N12 = bool.Parse(row["N12"].ToString());
               chamcong.N13 = bool.Parse(row["N13"].ToString());
               chamcong.N14 = bool.Parse(row["N14"].ToString());
               chamcong.N15 = bool.Parse(row["N15"].ToString());
               chamcong.N16 = bool.Parse(row["N16"].ToString());
               chamcong.N17 = bool.Parse(row["N17"].ToString());
               chamcong.N18 = bool.Parse(row["N18"].ToString());
               chamcong.N19 = bool.Parse(row["N19"].ToString());
               chamcong.N20 = bool.Parse(row["N20"].ToString());
               chamcong.N21 = bool.Parse(row["N21"].ToString());
               chamcong.N22 = bool.Parse(row["N22"].ToString());
               chamcong.N23 = bool.Parse(row["N23"].ToString());
               chamcong.N24 = bool.Parse(row["N24"].ToString());
               chamcong.N25 = bool.Parse(row["N25"].ToString());
               chamcong.N26 = bool.Parse(row["N26"].ToString());
               chamcong.N27 = bool.Parse(row["N27"].ToString());
               chamcong.N28 = bool.Parse(row["N28"].ToString());
               chamcong.N29 = bool.Parse(row["N29"].ToString());
               chamcong.N30 = bool.Parse(row["N30"].ToString());
               chamcong.N31 = bool.Parse(row["N31"].ToString());
               nhanvien.SubmitChanges();
               return true;
           }
           catch
           {
               return false;
           }
          

        }

        public int GetMaxThang(int thang,int nam)
        {
        
            var query = (from dt in nhanvien.ChamCongs where dt.Thang == thang && dt.Nam == nam select dt);
            if (query.Count() == 0)
                return thang;
            else
            {
                return -1;
            }

        }
       /// <summary>
       /// so ngay phai di lam trong mot thang
       /// </summary>
       /// <returns></returns>
       public int SoNgayPhaiDiLam(int year,int month)
       {
           int i = 0;
           DateTime first = new DateTime(year, month, 1);
           for (DateTime current = first; current.Month == first.Month; current = current.AddDays(1))
           {
               if (current.DayOfWeek.ToString() != "Sunday" && current.DayOfWeek.ToString() != "Saturday")
                   ++i;
           }
           return i;
       }
       public bool CreateChamCong(int thang,int nam,long nhanvienid)
       {
           try
           {
               ChamCong chamcong = new ChamCong()
               {
                   NhanVienID = nhanvienid,
                   Thang = thang,
                   Nam = nam,
                   N1 = false,
                   N2 = false,
                   N3 = false,
                   N4 = false,
                   N5 = false,
                   N6 = false,
                   N7 = false,
                   N8 = false,
                   N9 = false,
                   N10 = false,
                   N11 = false,
                   N12 = false,
                   N13 = false,
                   N14 = false,
                   N15 = false,
                   N16 = false,
                   N17 = false,
                   N18 = false,
                   N19 = false,
                   N20 = false,
                   N21 = false,
                   N22 = false,
                   N23 = false,
                   N24 = false,
                   N25 = false,
                   N26 = false,
                   N27 = false,
                   N28 = false,
                   N29 = false,
                   N30 = false,
                   N31 = false,
                   SoNgayDiLam = 0,
               };
               nhanvien.ChamCongs.InsertOnSubmit(chamcong);
               nhanvien.SubmitChanges();
               return true;
           }
           catch
           {
               return false;
           }
       }
    }
}
